/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.resources.joramratp.wrapper;

import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.control.LifeCycleController;
import org.objectweb.fractal.util.Fractal;

import com.scalagent.selfware.ServerFramework;

import fr.jade.base.JadeBaseComponent;
import fr.jade.fraclite.api.control.NoSuchAttributeException;

public class RATPAppWrapper extends JadeBaseComponent implements LifeCycleController {

  private String listAtt[] = {"serverName", "serverId"};
  
  private String serverName, serverId;

  private boolean started;

  public String getFcState() {
    return started ? LifeCycleController.STARTED : LifeCycleController.STOPPED;
  }

  public void startFc() throws IllegalLifeCycleException {
    if(!started){
      started = true;
      try{
        // Change our internal name
        Fractal.getNameController(self).setFcName("JndiRegistry");

        System.out.println("RATP App starting...");
        ServerFramework.main(new String[]{serverName, serverId});
        System.out.println("RATP App [started]");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  public void stopFc() throws IllegalLifeCycleException {
    if(started){
      System.out.println("RATP App stopping...");
      started = false;
      System.out.println("RATP App [stopped]");
    }
  }

  public String getAttribute(String name) throws NoSuchAttributeException {
    if (name.equals("serverName"))
      return this.serverName;
    if (name.equals("serverId"))
      return this.serverId;
    else
      return super.getAttribute(name);
  }

  public String[] listFcAtt() {
    return super.concat(listAtt, super.listFcAtt());
  }

  public void setAttribute(String name, String value) throws NoSuchAttributeException {
    if (name.equals("serverName"))
      this.serverName = value;
    else if (name.equals("serverId"))
      this.serverId = value;
    else
      super.setAttribute(name, value);
  }

}
